{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sort the Students by Their Kth Score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sortTheStudents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #根据第 K 场考试的分数排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>班里有 <code>m</code> 位学生，共计划组织 <code>n</code> 场考试。给你一个下标从 <strong>0</strong> 开始、大小为 <code>m x n</code> 的整数矩阵 <code>score</code> ，其中每一行对应一位学生，而 <code>score[i][j]</code> 表示第 <code>i</code> 位学生在第 <code>j</code> 场考试取得的分数。矩阵 <code>score</code> 包含的整数&nbsp;<strong>互不相同</strong>&nbsp;。</p>\n",
    "\n",
    "<p>另给你一个整数 <code>k</code> 。请你按第 <code>k</code> 场考试分数从高到低完成对这些学生（矩阵中的行）的排序。</p>\n",
    "\n",
    "<p>返回排序后的矩阵。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/11/30/example1.png\" style=\"width: 600px; height: 136px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>score = [[10,6,9,1],[7,5,11,2],[4,8,3,15]], k = 2\n",
    "<strong>输出：</strong>[[7,5,11,2],[10,6,9,1],[4,8,3,15]]\n",
    "<strong>解释：</strong>在上图中，S 表示学生，E 表示考试。\n",
    "- 下标为 1 的学生在第 2 场考试取得的分数为 11 ，这是考试的最高分，所以 TA 需要排在第一。\n",
    "- 下标为 0 的学生在第 2 场考试取得的分数为 9 ，这是考试的第二高分，所以 TA 需要排在第二。\n",
    "- 下标为 2 的学生在第 2 场考试取得的分数为 3 ，这是考试的最低分，所以 TA 需要排在第三。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/11/30/example2.png\" style=\"width: 486px; height: 121px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>score = [[3,4],[5,6]], k = 0\n",
    "<strong>输出：</strong>[[5,6],[3,4]]\n",
    "<strong>解释：</strong>在上图中，S 表示学生，E 表示考试。\n",
    "- 下标为 1 的学生在第 0 场考试取得的分数为 5 ，这是考试的最高分，所以 TA 需要排在第一。\n",
    "- 下标为 0 的学生在第 0 场考试取得的分数为 3 ，这是考试的最低分，所以 TA 需要排在第二。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m == score.length</code></li>\n",
    "\t<li><code>n == score[i].length</code></li>\n",
    "\t<li><code>1 &lt;= m, n &lt;= 250</code></li>\n",
    "\t<li><code>1 &lt;= score[i][j] &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>score</code> 由 <strong>不同</strong> 的整数组成</li>\n",
    "\t<li><code>0 &lt;= k &lt; n</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sort-the-students-by-their-kth-score](https://leetcode.cn/problems/sort-the-students-by-their-kth-score/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sort-the-students-by-their-kth-score](https://leetcode.cn/problems/sort-the-students-by-their-kth-score/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[10,6,9,1],[7,5,11,2],[4,8,3,15]]\\n2', '[[3,4],[5,6]]\\n0']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score,key= lambda x: -x[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = itemgetter(k), reverse = True)\n",
    "        return score\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        self.merge_sort_by_column(score, k)\n",
    "        return score\n",
    "\n",
    "    def merge_sort_by_column(self, arr, col):\n",
    "        if len(arr) > 1:\n",
    "            mid = len(arr) // 2\n",
    "            left_half = arr[:mid]\n",
    "            right_half = arr[mid:]\n",
    "\n",
    "            # 递归地对左右两半进行归并排序\n",
    "            self.merge_sort_by_column(left_half, col)\n",
    "            self.merge_sort_by_column(right_half, col)\n",
    "\n",
    "            i = j = k = 0\n",
    "\n",
    "            # 归并排序合并操作\n",
    "            while i < len(left_half) and j < len(right_half):\n",
    "                if left_half[i][col] > right_half[j][col]:\n",
    "                    arr[k] = left_half[i]\n",
    "                    i += 1\n",
    "                else:\n",
    "                    arr[k] = right_half[j]\n",
    "                    j += 1\n",
    "                k += 1\n",
    "\n",
    "            while i < len(left_half):\n",
    "                arr[k] = left_half[i]\n",
    "                i += 1\n",
    "                k += 1\n",
    "\n",
    "            while j < len(right_half):\n",
    "                arr[k] = right_half[j]\n",
    "                j += 1\n",
    "                k += 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score, key = lambda x : - x[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda v: -v[k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score, key = lambda score: -score[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = lambda x:-x[k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda s: -s[k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        \n",
    "        ls = []\n",
    "\n",
    "        for i, row in enumerate(score):\n",
    "            ls.append((row[k], i))\n",
    "        \n",
    "        ls.sort(key = lambda x:(-x[0]))\n",
    "\n",
    "        res = []\n",
    "\n",
    "        for a, b in ls:\n",
    "            res.append(score[b])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda x:-x[k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        k_score=[i[k] for i in score]\n",
    "        n=len(k_score)\n",
    "        for i in range(n):\n",
    "            for j in range(n-i-1):\n",
    "                if k_score[j]<k_score[j+1]:\n",
    "                    k_score[j],k_score[j+1]=k_score[j+1],k_score[j]\n",
    "                    score[j],score[j+1]=score[j+1],score[j]\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score, key=lambda score:score[:][k], reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        col_k = [(row, score[row][k]) for row in range(len(score))]\n",
    "        res = []\n",
    "        for i in sorted(col_k, key=lambda x: x[1], reverse=True):\n",
    "            res.append(score[i[0]])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        m=len(score)\n",
    "        scores=[score[i][k] for i in range(m)]\n",
    "        indices=list(range(m))\n",
    "        indices.sort(key=lambda x:scores[x], reverse=True)\n",
    "        return [score[i] for i in indices]\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: list[list[int]], k: int) -> list[list[int]]:\n",
    "        Col_K = [row[k] for row in score]\n",
    "        Col_K.sort(reverse=True)\n",
    "        res=[]\n",
    "        for i in range(len(Col_K)):\n",
    "            for j in range(len(score)):\n",
    "                if Col_K[i]==score[j][k]:\n",
    "                    res.append(score[j])\n",
    "                    score.pop(j)\n",
    "                    break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        list_k = list()\n",
    "        for line in score:\n",
    "            di = dict()\n",
    "            di[line[k]] = line\n",
    "            di[\"key\"] = line[k]\n",
    "            list_k.append(di)\n",
    "        list_k.sort(key=lambda key: key[\"key\"], reverse=True)\n",
    "        res = list()\n",
    "        for line in list_k:\n",
    "            res.append(line.get(line[\"key\"]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = lambda row: row[k], reverse = True)\n",
    "        return score\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        list_col = list(zip(*score))\n",
    "        new_score = [[]] * len(score)\n",
    "\n",
    "        list_k = list(list_col[k]).copy()\n",
    "        sort_k = sorted(list_k, reverse=True)\n",
    "        for i in range(0, len(score)):\n",
    "            j = sort_k.index(score[i][k])\n",
    "            new_score[j] = score[i]\n",
    "\n",
    "        return new_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda s : s[k] , reverse = True )\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        self.merge_sort_by_column(score, k)\n",
    "        return score\n",
    "\n",
    "    def merge_sort_by_column(self, arr, col):\n",
    "        if len(arr) > 1:\n",
    "            mid = len(arr) // 2\n",
    "            left_half = arr[:mid]\n",
    "            right_half = arr[mid:]\n",
    "\n",
    "            # 递归地对左右两半进行归并排序\n",
    "            self.merge_sort_by_column(left_half, col)\n",
    "            self.merge_sort_by_column(right_half, col)\n",
    "\n",
    "            i = j = k = 0\n",
    "\n",
    "            # 归并排序合并操作\n",
    "            while i < len(left_half) and j < len(right_half):\n",
    "                if left_half[i][col] > right_half[j][col]:\n",
    "                    arr[k] = left_half[i]\n",
    "                    i += 1\n",
    "                else:\n",
    "                    arr[k] = right_half[j]\n",
    "                    j += 1\n",
    "                k += 1\n",
    "\n",
    "            if i < len(left_half):\n",
    "                arr[k:] = left_half[i:]\n",
    "            else:\n",
    "                arr[k:] = right_half[j:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda s: -s[k])\n",
    "        return score\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        def takeK(list):\n",
    "            return list[k]\n",
    "        def cp(lista, listb):\n",
    "            return listb[k] - lista[k]\n",
    "        score.sort(key = takeK, reverse = True)\n",
    "        return score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = lambda x : x[k] , reverse = True)\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        \n",
    "        return sorted(score, key=lambda x: x[k], reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score,key=lambda row:row[k],reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        row = len(score)\n",
    "\n",
    "        for i in range(row-1):\n",
    "            for j in range(i+1, row):\n",
    "                if score[i][k] < score[j][k]:\n",
    "                    score[i], score[j] =  score[j], score[i]\n",
    "        \n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda x: x[k], reverse=True)\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda x:x[k], reverse=True)\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        m, n = len(score), len(score[0])\n",
    "        for i in range(m):\n",
    "            for j in range(i,0, -1):\n",
    "                if score[j][k] > score[j-1][k]:\n",
    "                    score[j], score[j-1] = score[j-1], score[j]\n",
    "                else:\n",
    "                    break\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = itemgetter(k), reverse = True)\n",
    "        return score\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda row: row[k], reverse=True)\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "\n",
    "        score.sort(key = lambda s: -s[k])\n",
    "        return score\n",
    "\n",
    "\n",
    "# class Solution:\n",
    "\n",
    "#     def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "\n",
    "#         m = len(score)\n",
    "#         n = len(score[0])\n",
    "#         temp = list()\n",
    "#         for i in range(m-1):\n",
    "#             for j in range(i,m):\n",
    "#                 if score[i][k] < score[j][k]:\n",
    "#                     temp = score[i] \n",
    "#                     score[i] = score[j]\n",
    "#                     score[j] = temp\n",
    "#         return score\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score, key=lambda x: x[k], reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda row : row[k] , reverse = True )\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        # 使用lambda函数按照第k个元素对score进行排序\n",
    "        score.sort(key=lambda x: x[k], reverse=True)\n",
    "        return score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        # res = sorted(score, key = lambda x : x[k], reverse = True)\n",
    "        # print(res)\n",
    "\n",
    "        return sorted(score, key = lambda x: x[k], reverse = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        s = score[:]\n",
    "        s.sort(key=lambda x:x[k], reverse=True)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        m,n=len(score),len(score[0])\n",
    "        nums=[]\n",
    "        for i in range(m):\n",
    "            nums.append((score[i][k],i))\n",
    "        nums.sort(key=lambda x:-x[0])\n",
    "        res=[]\n",
    "        for a,b in nums:\n",
    "            res.append(score[b])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = lambda row: row[k], reverse = True)\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda s: -s[k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda s: -s[k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score,key = lambda x: x[k],reverse = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score,key=lambda v:v[k],reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "\n",
    "        return sorted(score,key=lambda x:x[k],reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        helper=[]\n",
    "        for i in range(len(score)):\n",
    "            helper.append((score[i][k],i))\n",
    "        # quick sort\n",
    "\n",
    "        self.quick_sort(helper,0,len(score)-1)\n",
    "\n",
    "\n",
    "        ans=[]\n",
    "        for i in range(len(score)-1,-1,-1):\n",
    "            ans.append(score[helper[i][1]])\n",
    "        return ans\n",
    "\n",
    "    \n",
    "    def quick_sort(self,helper,begin,end):\n",
    "        if begin>=end:\n",
    "            return\n",
    "\n",
    "        mil=helper[begin]\n",
    "        i=begin\n",
    "        j=end\n",
    "        flag=0\n",
    "        while i!=j:\n",
    "            if flag==0:\n",
    "                if mil[0]>helper[j][0]:\n",
    "                    helper[i]=helper[j]\n",
    "                    flag=1\n",
    "                    i+=1\n",
    "                else:\n",
    "                    j-=1\n",
    "            else:\n",
    "                if mil[0]<helper[i][0]:\n",
    "                    helper[j]=helper[i]\n",
    "                    flag=0\n",
    "                    j-=1\n",
    "                else:\n",
    "                    i+=1\n",
    "\n",
    "        helper[i]=mil\n",
    "        self.quick_sort(helper,begin,i)\n",
    "        self.quick_sort(helper,i+1,end)\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score,key= lambda x: -x[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = lambda row: row[k], reverse = True)\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = lambda row: row[k], reverse = True)\n",
    "        return score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "       score.sort(key=lambda x:x[k],reverse=True)\n",
    "       return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        m = len(score)\n",
    "        dic = {}\n",
    "        for i in range(m):\n",
    "            dic[i] = score[i][k]\n",
    "        sorted_dic = sorted(dic.items(), key=(lambda x: (-x[1])))\n",
    "        for x, y in sorted_dic:\n",
    "            res.append(score[x])\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "'''\n",
    "每行是一位写生，每一列是相应的一场考试，相应的位置的值是这个考试的分数\n",
    "\n",
    "一个特性是：每个分数都不同\n",
    "\n",
    "根据某一的大小关系对行进行排序，也就是又一个结果数组res 将这个考试的分数高的学生首先append道这个res中\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score,key=lambda x: -x[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        ret = sorted(score, key=lambda x:x[k], reverse=True)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = lambda x:x[k], reverse = True)\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score, key=lambda x:-x[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda x:-x[k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score, key=lambda x: -x[k])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "\n",
    "        score.sort(reverse = True, key = lambda x : x[k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda s: -s[k])\n",
    "        return score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        dic = {}\n",
    "        res = []\n",
    "        for i in range(len(score)):\n",
    "            dic[score[i][k]] = score[i]\n",
    "        s = sorted(dic, reverse=True)\n",
    "        for i in range(len(s)):\n",
    "            res.append(dic[s[i]])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, s: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(s,key=lambda x: -x[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key = lambda x: -x[:][k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score = sorted(score, key = lambda x:x[k])[::-1]\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score,key=lambda i:i[k],reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        # return sorted(score, key = lambda score : -score[k])\n",
    "\n",
    "        def fast_sort(lst):\n",
    "            if len(lst) < 2:\n",
    "                return lst\n",
    "            pivot = lst[len(lst) // 2]\n",
    "            more = [i for i in lst if i > pivot]\n",
    "            less = [i for i in lst if i < pivot]\n",
    "            equal = [i for i in lst if i == pivot]\n",
    "            return fast_sort(more) + equal + fast_sort(less)\n",
    "        transposed_score = list(zip(*score))\n",
    "        score_rank = transposed_score[k]\n",
    "        score_mapping_index = dict(zip(score_rank, range(len(score_rank))))\n",
    "\n",
    "        sorted_rank = fast_sort(score_rank)\n",
    "\n",
    "        ans = []\n",
    "        for i in sorted_rank:\n",
    "            ans.append(score[score_mapping_index[i]])\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        return sorted(score, key=lambda x: x[k], reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        a=[]\n",
    "        b=[]\n",
    "        for i in range(len(score)):\n",
    "                a.append(score[i][k])\n",
    "                a.sort(reverse=True)\n",
    "               \n",
    "        for j in a:\n",
    "          for i in range(len(score)):\n",
    "            if j==score[i][k]:\n",
    "                b.append(score[i])\n",
    "        return b        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "\n",
    "        return sorted(score, key=lambda x: x[k], reverse=True)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        n=len(score)\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                if score[i][k]<score[j][k]:\n",
    "                   a=score[i]\n",
    "                   score[i]=score[j]\n",
    "                   score[j]=a\n",
    "        \n",
    "        return score\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda it: -it[k])\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        score.sort(key=lambda row: row[k], reverse=True)\n",
    "\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:\n",
    "        a, b = [], []\n",
    "        \n",
    "        for i in range(len(score)) :\n",
    "            a.append(score[i][k])\n",
    "            a.sort(reverse = True)\n",
    "               \n",
    "        for j in a :\n",
    "            for i in range(len(score)) :\n",
    "                if j == score[i][k] :\n",
    "                    b.append(score[i])\n",
    "        return b        "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
